package com.llkj.qingnianguoxue.home;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;

/**
 * 范儿活动
 */
public class PreviousActionActivity extends BaseActivity implements View.OnClickListener {

    private FrameLayout flPayTag,flNoPayTag;
    private ActionFragment mPayFragment,mNoPayFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_previous_action);
        setTitle("范儿活动", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);

        initView();
        initListener();

        setSelection(0);
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        flPayTag = (FrameLayout) findViewById(R.id.fl_pay);
        flPayTag.setSelected(true);
        flNoPayTag = (FrameLayout) findViewById(R.id.ll_pay_no);
    }

    private void initListener() {
        flPayTag.setOnClickListener(this);
        flNoPayTag.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fl_pay:
                if(!flPayTag.isSelected()){
                    setSelection(0);
                }
                break;
            case R.id.ll_pay_no:
                if(!flNoPayTag.isSelected()){
                    setSelection(1);
                }
                break;
        }
    }

    private void setSelection(int index) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        switch (index) {
            case 0://
                setSelected(flPayTag);
                if (mPayFragment == null) {
                    mPayFragment = ActionFragment.newInstance(1);
                    fragmentTransaction.add(R.id.content_layout, mPayFragment, "tab1");
                } else {
                    fragmentTransaction.show(mPayFragment);
                }
                fragmentTransaction.commit();
                break;
            case 1://
                setSelected(flNoPayTag);
                if (mNoPayFragment == null) {
                    mNoPayFragment = ActionFragment.newInstance(2);
                    fragmentTransaction.add(R.id.content_layout, mNoPayFragment, "tab2");
                } else {
                    fragmentTransaction.show(mNoPayFragment);
                }

                fragmentTransaction.commit();
                break;
        }
    }

    private void setSelected(View view) {
        flPayTag.setSelected(false);
        flNoPayTag.setSelected(false);
        view.setSelected(true);
    }

    /**
     * 隐藏所有的fragment
     *
     * @param transaction
     */
    private void hideFragment(FragmentTransaction transaction) {
        if (mPayFragment != null) {
            transaction.hide(mPayFragment);
        }
        if (mNoPayFragment != null) {
            transaction.hide(mNoPayFragment);
        }
    }
}
